<template>
  <div class="select-book">
    <div class="search">
      <div class="icon">
        <img src="../../../../assets/ss.svg" alt="" />
      </div>
      <input type="text" v-model="search" placeholder="请输入武将名称" />
      <div class="clear" v-if="search" @click="search = ''">
        <img src="../../../../assets/clear.svg" alt="" />
      </div>
    </div>
    <collapse-transition>
      <div class="type" v-show="!search.trim()">
        <div
          class="item"
          @click="selectType(1)"
          :class="{ active: filter.type.indexOf(1) !== -1 }"
        >
          作战
        </div>
        <div
          class="item"
          @click="selectType(2)"
          :class="{ active: filter.type.indexOf(2) !== -1 }"
        >
          虚实
        </div>
        <div
          class="item"
          @click="selectType(3)"
          :class="{ active: filter.type.indexOf(3) !== -1 }"
        >
          军形
        </div>
        <div
          class="item"
          @click="selectType(4)"
          :class="{ active: filter.type.indexOf(4) !== -1 }"
        >
          九变
        </div>
      </div>
    </collapse-transition>
    <div class="books-list">
      <vue-scroll :ops="ops">
        <div class="list">
          <div
            class="book"
            :class="{
              active: bookIsSelect(book.id),
              dis: bookIsSelect(book.id) !== fill,
            }"
            @click="clickBook(book)"
            v-for="(book, i) in filterList"
            :key="i"
            v-html="book.name"
          ></div>
        </div>
      </vue-scroll>
    </div>
  </div>
</template>

<script>
/**
1.阵容编辑页面
2.阵容分享页面
3.阵容战法兵书去重逻辑 OK
4.去除阵容thought OK
 */
import { mapState } from "vuex";
import Book from "@/object/Book";
export default {
  name: "SelectBook",
  props: {
    value: Object,
    fill: String,
  },
  model: { prop: "value", event: "change" },
  computed: {
    ...mapState("data", ["heroesList", "booksList"]),
    filterList() {
      let list = this.booksList.map((item) => {
        return new Book(JSON.parse(JSON.stringify(item)));
      });
      // 判断当前是第几个兵书
      if (this.isFirst()) {
        list = list.filter((item) => item.name.length == 4);
      } else {
        list = list.filter((item) => item.name.length == 2);

        // 判断第一个选中的是什么
        let book = this.getOneBook();
        if (book) {
          list = list.filter((item) => item.type == book.type);
        }
      }

      if (this.search.trim()) {
        list = list.filter((item) => {
          if (item.name.indexOf(this.search.trim()) !== -1) {
            var reg = new RegExp(this.search.trim(), "g");
            item.name = item.name.replace(
              reg,
              "<span>" + this.search.trim() + "</span>"
            );
            return true;
          } else return false;
        });
        return list;
      } else {
        // 战法类型
        if (this.filter.type.length) {
          list = list.filter((item) => {
            return this.filter.type.indexOf(item.type) !== -1;
          });
        }

        return list;
      }
    },
  },
  data() {
    return {
      one_book: null,
      search: "",
      ops: {
        scrollPanel: {
          scrollingX: false,
          scrollingY: true,
        },
        bar: {
          showDelay: 500,
          onlyShowBarOnScroll: true,
          keepShow: false,
          background: "#c1c1c1",
          opacity: 1,
          hoverStyle: false,
          specifyBorderRadius: false,
          minSize: false,
          size: "5px",
          disable: false,
        },
      },
      filter: {
        show: false,
        type: [1],
      },
    };
  },
  methods: {
    isFirst() {
      let arr = ["chef_book_one", "sub_one_book_one", "sub_two_book_one"];
      if (arr.indexOf(this.fill) === -1) return false;
      return true;
    },
    getOneBook() {
      let fill = this.fill,
        val = this.value;
      let book;
      if (fill.indexOf("chef_book") !== -1) {
        if (fill !== "chef_book_one") book = this.bookById(val.chef_book_one);
      } else if (fill.indexOf("sub_one_book") !== -1) {
        if (fill !== "sub_one_book_one")
          book = this.bookById(val.sub_one_book_one);
      } else if (fill.indexOf("sub_two_book") !== -1) {
        if (fill !== "sub_two_book_one")
          book = this.bookById(val.sub_two_book_one);
      }
      return book;
    },
    bookById(id) {
      let book;
      this.booksList.some((item) => {
        if (item.id === id) book = item;
        return !!book;
      });
      return book;
    },
    selectType(type) {
      let book = this.getOneBook();
      if (book && book.type !== type) return;
      this.filter.type = [type];
    },
    selectQuality(quality) {
      if (this.filter.quality.indexOf(quality) === -1) {
        this.filter.quality.push(quality);
      } else {
        this.filter.quality.splice(this.filter.quality.indexOf(quality), 1);
      }
    },
    // 战法是否被选中
    bookIsSelect(id) {
      let chef = ["chef_book_one", "chef_book_two", "chef_book_three"];
      let sub1 = ["sub_one_book_one", "sub_one_book_two", "sub_one_book_three"];
      let sub2 = ["sub_two_book_one", "sub_two_book_two", "sub_two_book_three"];

      let heros = [chef, sub1, sub2];

      let select = "";
      // 判断当前对应的武将
      heros.some((item) => {
        if (item.indexOf(this.fill) !== -1) {
          item.some((name) => {
            if (this.value[name] === id) select = name;
            return !!select;
          });
          return !!select;
        }
      });

      return select;
    },
    // 点击武将
    clickBook(book) {
      // 判断武将是否被其他位置选中
      let select = this.bookIsSelect(book.id);
      if (select && select !== this.fill) return;
      let value = JSON.parse(JSON.stringify(this.value));
      if (book.id === value[this.fill]) {
        value[this.fill] = null;
        this.$emit("change", value);
      } else {
        value[this.fill] = book.id;
        this.$emit("change", value);
        this.$emit("next");
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.select-book {
  flex: 1;
  height: 0;
  margin-top: 0.1rem;
  border-radius: 4px;
  border: 1px solid #ebebeb;
  padding: 0.1rem 0.1rem 0.01rem;
  display: flex;
  flex-direction: column;
  background: #fff;
  .search {
    height: 0.34rem;
    border-radius: 4px;
    border: 1px solid #ebebeb;
    display: flex;
    align-items: center;
    input {
      flex: 1;
      width: 0;
      border: none;
      font-size: 0.14rem;
      color: #233d4d;
      &::placeholder {
        font-size: 0.12rem;
        color: #cccccc;
      }
    }
    .icon {
      width: 0.34rem;
      height: 0.34rem;
      display: flex;
      align-items: center;
      justify-content: center;
      img {
        width: 0.24rem;
      }
    }
    .clear {
      width: 0.3rem;
      height: 0.34rem;
      display: flex;
      align-items: center;
      justify-content: center;
      cursor: pointer;
      img {
        width: 0.22rem;
      }
    }
  }
  .type {
    display: flex;
    align-items: center;
    margin-top: 0.07rem;
    position: relative;
    justify-content: space-between;
    padding-bottom: 0rem;
    &.pb {
      padding-bottom: 0.28rem;
    }
    .item {
      width: 0.72rem;
      height: 0.28rem;
      background: #f2f2f2;
      border-radius: 0.05rem;
      box-sizing: border-box;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 0.12rem;
      &.active {
        background: #fff;
        border: 1px solid #ff8a69;
        background-image: url(../../../../assets/right_close.svg);
        background-repeat: no-repeat;
        background-position: right top;
      }
    }
    .flex {
      flex: 1;
    }
    .filter {
      position: absolute;
      width: 0.56rem;
      height: 0.28rem;
      top: 0.34rem;
      right: 0;
      border-radius: 0.04rem;
      box-sizing: border-box;
      display: flex;
      justify-content: center;
      align-items: center;
      border: 1px solid #ebebeb;
      font-size: 0.12rem;
      color: #233d4d;
      svg {
        margin-left: 0.06rem;
        width: 0.06rem;
        height: 0.04rem;
        transform: rotate(180deg);
        transition: all 0.3s;
      }
      &.active {
        color: #ff8a69;
        border: 1px solid #ff8a69;
        svg {
          transform: rotate(0deg);
        }
      }
    }
  }
  .quality {
    margin-top: 0.15rem;
    .tit {
      font-size: 0.12rem;
      color: #233d4d;
      line-height: 0.17rem;
    }
    .list {
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-top: 0.05rem;
      .item {
        width: 0.72rem;
        height: 0.28rem;
        background: #f2f2f2;
        border-radius: 0.05rem;
        box-sizing: border-box;
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 0.12rem;
        &.active {
          background: #fff;
          border: 1px solid #ff8a69;
          background-image: url(../../../../assets/right_close.svg);
          background-repeat: no-repeat;
          background-position: right top;
        }
      }
    }
  }
  .books-list {
    margin-top: 0.07rem;
    height: 0;
    flex: 1;
    .book {
      width: 0.72rem;
      height: 0.28rem;
      background: #ffffff;
      border-radius: 4px;
      border: 1px solid #ebebeb;
      box-sizing: border-box;
      display: inline-flex;
      align-items: center;
      justify-content: center;
      font-size: 0.12rem;
      margin: 0.08rem 0 0;
      margin-left: 0.06rem;
      color: #233d4d;
      &.book:nth-child(4n + 1) {
        margin-left: 0;
      }
      &.active {
        color: #fff !important;
        background: #ff8a69;
        border: 1px solid #ff8a69;
        &.dis {
          color: #fff;
          background: #cccccc;
          border: 1px solid #cccccc;
        }
        ::v-deep {
          span {
            color: #fff !important;
          }
        }
      }
      ::v-deep {
        span {
          color: #ff8a69;
        }
      }
    }
  }
}
</style>
